package com.ideone.cli;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.crypto.spec.IvParameterSpec;

public class Main {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static String getExt(String fl){
		int dot = fl.lastIndexOf(".");
		return fl.substring(dot);
	}
	static HashMap<String,Integer> hm;
	static HashMap<String,String> sniffRules;
	public static void initSniffRules(){
		sniffRules = new HashMap<String,String>();
		sniffRules.put(".cpp", "c++ ");
		sniffRules.put(".pl", "perl ");
		sniffRules.put(".py", "python ");
		sniffRules.put(".java", "java ");
		sniffRules.put(".c", "gcc");
		sniffRules.put(".asm", "Assembler");
		sniffRules.put(".rb", "ruby ");
		sniffRules.put(".hs", "Haskell ");
		sniffRules.put(".php", "php ");
		sniffRules.put(".sh", "bash ");
		
		
	}
	public static int getLang(String fl){
		String ext = getExt(fl);
		if(ext!=null){
		    String toSniff = sniffRules.get(ext.toLowerCase());
		    
		}
		return -1;
	}
	public static InputParams populateInputParams(String [] args){
		InputParams params = InputParams.getInstance();
		params.setUser(System.getenv(Constants.USER_ENV));
		params.setPasswd(System.getenv(Constants.PASSWD_ENV));
		params.setInputFile(System.getenv(Constants.INPUT_FILE_ENV));
		for(int i=0;i<args.length;i++){
			if(Constants.INPUT_FILE_ENV.equals(args[i])){
				params.setInputFile(args[i+1]);
				i++;
			} else if(Constants.LANG_FLAG.equals(args[i])){
				params.setLanguage(Integer.parseInt(args[i+1]));
			} else if(Constants.LIST_LANG_FLAG.equals(args[i])){
				params.setListLanguages(true);
			} else {
				params.setSourceCodeFile(args[i]);
			}
		}
		return params;
	}
	public static void printOutput(Object obj){
		if(obj!=null){
			String str = (String) obj;
			if(str.trim().length()>0){
				System.out.println(str);
			}
		}
	}
	public static void prettyPrintLang(HashMap hm){
		Integer code[] = new Integer[hm.size()];
		String compiler[] = new String[hm.size()];
		int i=0;
		for(Iterator it = hm.entrySet().iterator();it.hasNext();){
    		Map.Entry me = (Map.Entry) it.next();
    		code[i] = (Integer) me.getKey();
    		compiler[i] = (String) me.getValue();
    		i++;
    	}
		System.out.printf("|  Code | %-50s |\n", "Compiler");
		for(int j=0;j<code.length;j++){
			System.out.printf("| %5d | %-50s |\n", code[j],compiler[j]);
		}
		
	}
	public static String readLocalFile(String file) throws IOException{
		  FileInputStream fis = new FileInputStream(file);
	        byte b[] = new byte[fis.available()];
	        fis.read(b);
	        String str = new String(b);
	        return str;
	}
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		initSniffRules();
		InputParams params = populateInputParams(args);
		hm = new HashMap<String,Integer>();
		hm.put(".cpp",1);
		hm.put(".java",10);
       
        
        IdeOneService service = new IdeOneService(params.getUser(),params.getPasswd());
        if(params.isListLanguages()||params.getLanguage()<0){
        	HashMap langsmap = service.getLanguages();
        	prettyPrintLang(langsmap);
        	
        	System.exit(0);
        }
        
        String str = readLocalFile(params.getSourceCodeFile());
        String progInput="";
        try{
			if (params.getInputFile() != null) {
				progInput = readLocalFile(params.getInputFile());
			}
        }catch(Exception e){
        	
        }
        int lang = getLang(params.getSourceCodeFile());
        HashMap res = service.createSubmission(str, params.getLanguage(), progInput, true, true);
        String link = (String) res.get("link");
        int tries =4;
        do {
        	res = service.getSubmissionStatus(link);
        	tries--;
        }while(!"0".equals(res.get("status")+"")&&tries>0);
        res = service.getSubmissionDetails(link, false, false, true, true, true);
        Object cmpInfo = res.get("cmpinfo");
        Object stderr = res.get("stderr");
        Object output = res.get("output");
		printOutput(res.get("cmpinfo"));

		printOutput(res.get("stderr"));

		printOutput(res.get("output"));
		
	}

}
